package by.gravity.grsu.shedule;

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import by.gravity.grsu.shedule.provider.SheduleProvider;
import by.gravity.grsu.shedule.provider.listener.OnLoadStringArrayListener;

public class SettingsActivity extends PreferenceActivity {

	private SheduleProvider mProvider;

	private ListPreference mSpecialityPreference;

	private ListPreference mKursPreference;

	private ListPreference mGroupPreference;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preference);

		mProvider = new SheduleProvider(this);

		mSpecialityPreference = (ListPreference) findPreference(getString(R.string.speciality));
		mKursPreference = (ListPreference) findPreference(getString(R.string.kurs));
		mGroupPreference = (ListPreference) findPreference(getString(R.string.group));

		initPreference();

		mSpecialityPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

			@Override
			public boolean onPreferenceChange(Preference paramPreference, Object paramObject) {
				String specName = paramObject.toString();
				if (!specName.equals(getSelectedSpecName())) {
					clearKurs();
					clearGroup();
				}
				setSelectedSpecName(specName);
				loadKurs(specName);
				return true;
			}
		});

		mKursPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

			@Override
			public boolean onPreferenceChange(Preference paramPreference, Object paramObject) {
				String groupName = paramObject.toString();
				if (!groupName.equals(getSelectedKursName())) {
					clearGroup();
				}
				setSelectedKursName(groupName);
				loadGroup(mSpecialityPreference.getValue(), groupName);
				return true;
			}
		});

		mGroupPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

			@Override
			public boolean onPreferenceChange(Preference paramPreference, Object paramObject) {
				setSelectedGroupName(paramObject.toString());
				return true;
			}
		});

	}

	private void initPreference() {
		loadSpeciality();
		loadKurs();
		loadGroup();

		if (getSelectedSpecName() != null) {
			setSelectedSpecName(getSelectedSpecName());
		}

		if (getSelectedKursName() != null) {
			setSelectedKursName(getSelectedKursName());
		}
		if (getSelectedGroupName() != null) {
			setSelectedGroupName(getSelectedGroupName());
		}

	}

	private void loadSpeciality() {
		mProvider.getSpecialityList(new OnLoadStringArrayListener() {

			@Override
			public void onComplete(String[] result) {
				mSpecialityPreference.setEntries(result);
				mSpecialityPreference.setEntryValues(result);
			}

			@Override
			public void onError() {

			}

		});
	}

	private void loadKurs() {
		String specName = getSelectedSpecName();

		loadKurs(specName);
	}

	private void loadKurs(String specName) {
		if (specName == null) {
			return;
		}

		mProvider.getKursList(specName, new OnLoadStringArrayListener() {

			@Override
			public void onError() {

			}

			@Override
			public void onComplete(String[] result) {
				mKursPreference.setEntries(result);
				mKursPreference.setEntryValues(result);

			}
		});

	}

	private void loadGroup() {
		String specName = getSelectedSpecName();
		String kursName = getSelectedKursName();

		loadGroup(specName, kursName);
	}

	private void loadGroup(String specName, String kursName) {
		if (specName == null || kursName == null) {
			return;
		}
		mProvider.getGroupList(specName, kursName, new OnLoadStringArrayListener() {

			@Override
			public void onError() {

			}

			@Override
			public void onComplete(String[] result) {
				mGroupPreference.setEntries(result);
				mGroupPreference.setEntryValues(result);

			}
		});
	}

	private String getSelectedSpecName() {
		return mSpecialityPreference.getValue();
	}

	private void setSelectedSpecName(String name) {
		mSpecialityPreference.setSummary(name);
	}

	private String getSelectedKursName() {
		return mKursPreference.getValue();
	}

	private void setSelectedKursName(String name) {
		mKursPreference.setSummary(name);
	}

	private String getSelectedGroupName() {
		return mGroupPreference.getValue();
	}

	private void setSelectedGroupName(String name) {
		mGroupPreference.setSummary(name);
	}

	private void clearKurs() {
		mKursPreference.setSummary(getString(R.string.kurs_not_selected));
		mKursPreference.setValue(null);
	}

	private void clearGroup() {
		mGroupPreference.setSummary(getString(R.string.group_not_selected));
		mGroupPreference.setValue(null);
	}
}
